<?php
/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2018/1/26
 * Time: 16:48
 */

namespace App\Services\Category;


use App\Repositories\Category\CategoryRepository;
use Illuminate\Support\Facades\Cache;

class CategoryService
{
    /**
     * @var CategoryRepository
     */
    protected $categoryRepository;

    /**
     * @var
     */
    private $category_list;

    /**
     * CategoryService constructor.
     * @param CategoryRepository $categoryRepository
     */
    public function __construct(CategoryRepository $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

    /**
     * 获取所有子分类id
     * @author yangzhic
     * @param $cateId
     * @return array
     */
    public function getAllSubCateId($cateId){

        $sub_cate_id = Cache::get('category_sub_cat_id_'.$cateId);

        if(empty($sub_cate_id)){
            $categories = $this->categoryRepository->getAllCategory();

            $sub_cate_id = $this->getSubs($categories,$cateId,'cat_id');

            $sub_cate_id[] = $cateId;
            //去重
            $sub_cate_id = array_unique($sub_cate_id);

            Cache::put('category_sub_cat_id_'.$cateId,$sub_cate_id,60);
        }

        return $sub_cate_id;
    }

    /**
     * 根据分类id获取分类名称
     * @author Min
     * @param $cat_id
     * @return mixed
     */
    public function getCatNameByCatId($cat_id){
        return $this->categoryRepository->getCatNameByCatId($cat_id);
    }

    /**
     * 获取分类列表
     * @author Min
     * @param $cat_id
     * @return mixed
     */
    public function getCategoryListByCatId($cat_id){
        if(is_array($cat_id)){
            return $this->categoryRepository->getCategoryListInCatId($cat_id);
        }else{
            return $this->categoryRepository->getCategoryListByCatId($cat_id);
        }
    }

    /**
     * 获取某个分类的所有父级分类
     * @author Min
     * @param $cat_id
     * @return array
     */
    public function getCategoryParentCatId($cat_id){
        $category = $this->getCategoryListByCatId($cat_id);

        foreach ($category as $value){
            $this->category_list[] = $value;
            if($value['parent_id']){
                $this->getCategoryParentCatId($value['parent_id']);
            }
        }
        return $this->category_list;
    }

    /**
     * 获取某个分类的所有子分类
     * @author yangzhic
     * @param $categories
     * @param int $cate_id
     * @param string $field
     * @param int $level
     * @return array
     */
    public function getSubs($categories,$cate_id=0,$field="",$level=1){
        $subs=array();
        foreach($categories as $item){
            if($item['parent_id']==$cate_id){
                if(empty($field)){
                    $item['level']=$level;
                    $subs[]=$item;
                }else{
                    $subs[]=$item[$field];
                }
                $subs=array_merge($subs,$this->getSubs($categories,$item[$field],$field,$level+1));
            }
        }
        return $subs;
    }

    /**
     * 获取分类级别id
     * @author Min
     * @param $catId  分类id
     * @param $level 级别 $level=1获取1级分类id
     * @return int
     */
    protected $cat_ids;
    public function findLevelCatId($catId, $level=1){
        $data = $this->categoryRepository->find($catId, ['cat_id','parent_id']);
        $this->cat_ids[] = $data['cat_id'];

        if($data['parent_id'])
            $this->findLevelCatId($data['parent_id']);

        krsort($this->cat_ids);
        $id_list = [];
        foreach ($this->cat_ids as $val){
            $id_list[] = $val;
        }

        return $id_list[$level-1];
    }
}